﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions;
using CrystalDecisions.Enterprise;
using CrystalDecisions.CrystalReports;
using CrystalDecisions.ReportSource;
using CrystalDecisions.CrystalReports.Engine;
using TI_BAL;
using CrystalDecisions.Shared;

namespace SecuritySystem.Reports.Datasets
{
    public partial class ReportFormSupplier : Form
    {
        TIEntities dbContext;
        public string ReportType = "";
        int ACCOUNTS_PAYABLE_ID = 5;

        public ReportFormSupplier()
        {
            InitializeComponent();
            dbContext = new TIEntities();
            FillSupplierCombo();
        }

        private void FillSupplierCombo()
        {

            List<Supplier> SupplierList = (from pt in dbContext.Suppliers select pt).ToList<Supplier>();
            if (SupplierList != null)
            {
                BindDataWithSupplierCombo(SupplierList);
            }
            else
            {
                SupplierList = new List<Supplier>();
            }

        }

        private void BindDataWithSupplierCombo(List<Supplier> SupplierList)
        {

            this.comboBoxSuppliers.DataSource = null;
            comboBoxSuppliers.DisplayMember = "CompanyName";
            comboBoxSuppliers.ValueMember = "ID";
            comboBoxSuppliers.DataSource = SupplierList;
            comboBoxSuppliers.Refresh();
        }

        private void ReportFormSupplier_Load(object sender, EventArgs e)
        {
            this.dateTimePickerEnd.Value = DateTime.Now;
            this.dateTimePickerStart.Value = DateTime.Now.Subtract(TimeSpan.FromDays(30));            
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                DateTime tempDate = this.dateTimePickerStart.Value.Subtract(TimeSpan.FromDays(1));
                DateTime dtStart = new DateTime(tempDate.Year, tempDate.Month, tempDate.Day);                

                tempDate = this.dateTimePickerEnd.Value.AddDays(1);
                DateTime dtEnd = new DateTime(tempDate.Year, tempDate.Month, tempDate.Day);

                int supplierId = int.Parse(this.comboBoxSuppliers.SelectedValue.ToString());
                //var ledgars = (from journal in dbContext.Journals where journal.Account_ID == ACCOUNTS_PAYABLE_ID select new { journal.Journal_Date, SupplierCompanyName = journal.Supplier.CompanyName, journal.Debit_Amount, journal.Credit_Amount }).ToList();
                var ledgars = (from journal in dbContext.Journals where journal.Journal_Date >= dtStart && journal.Journal_Date <= dtEnd && journal.Account_ID == ACCOUNTS_PAYABLE_ID && journal.Supplier_ID == supplierId select new 
                { Journal_Date = journal.Journal_Date.Value,
                  Journal_Description = journal.Journal_Description,
                    SupplierCompanyName = journal.Supplier.CompanyName, Debit_Amount = journal.Debit_Amount.Value, Credit_Amount = journal.Credit_Amount.Value,                
                   Product_Type = journal.Product_Type ?? " ", Sub_Type = journal.Sub_Type ?? " ",Product_Size = journal.Product_Size ?? " ", Quantity = journal.Quantity ?? 0, Per_Item_Price = journal.Per_Item_Price ?? 0}).ToList();


                SupplierLedgerReport reportobj = new SupplierLedgerReport();

                reportobj.SetDataSource(ledgars); ParameterFieldDefinitions crParameterFieldDefinitions;
                ParameterFieldDefinition crParameterFieldDefinition;
                ParameterValues crParameterValues = new ParameterValues();
                ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();

                crParameterDiscreteValue.Value = System.Configuration.ConfigurationSettings.AppSettings["CompanyName"];
                crParameterFieldDefinitions = reportobj.DataDefinition.ParameterFields;
                crParameterFieldDefinition = crParameterFieldDefinitions[0];
                crParameterValues = crParameterFieldDefinition.CurrentValues;

                crParameterValues.Clear();
                crParameterValues.Add(crParameterDiscreteValue);
                crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);

                ParameterFieldDefinitions crParameterFieldDefinitions1;
                ParameterFieldDefinition crParameterFieldDefinition1;
                ParameterValues crParameterValues1 = new ParameterValues();
                ParameterDiscreteValue crParameterDiscreteValue1 = new ParameterDiscreteValue();

                crParameterDiscreteValue1.Value = System.Configuration.ConfigurationSettings.AppSettings["CompanyAddress"];
                crParameterFieldDefinitions1 = reportobj.DataDefinition.ParameterFields;
                crParameterFieldDefinition1 = crParameterFieldDefinitions1[1];
                crParameterValues1 = crParameterFieldDefinition1.CurrentValues;

                crParameterValues1.Clear();
                crParameterValues1.Add(crParameterDiscreteValue1);
                crParameterFieldDefinition1.ApplyCurrentValues(crParameterValues1);



                this.crystalReportViewer1.ReportSource = reportobj;
            }
            catch { MessageBox.Show("Some Error Occured while generating Report"); }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                DateTime tempDate = this.dateTimePickerStart.Value.Subtract(TimeSpan.FromDays(1));
                DateTime dtStart = new DateTime(tempDate.Year, tempDate.Month, tempDate.Day);

                tempDate = this.dateTimePickerEnd.Value.AddDays(1);
                DateTime dtEnd = new DateTime(tempDate.Year, tempDate.Month, tempDate.Day);
              
                var ledgars = (from journal in dbContext.Journals where journal.Journal_Date >= dtStart && journal.Journal_Date <= dtEnd && journal.Account_ID == ACCOUNTS_PAYABLE_ID select new { Journal_Date = journal.Journal_Date.Value,
                    Journal_Description = journal.Journal_Description,
                    SupplierCompanyName = journal.Supplier.CompanyName, Debit_Amount = journal.Debit_Amount.Value, Credit_Amount = journal.Credit_Amount.Value,
                Product_Type = journal.Product_Type ?? " ", Sub_Type = journal.Sub_Type ?? " ",Product_Size = journal.Product_Size ?? " ", Quantity = journal.Quantity ?? 0, Per_Item_Price = journal.Per_Item_Price ?? 0}).ToList();


                SupplierLedgerReport reportobj = new SupplierLedgerReport();

                reportobj.SetDataSource(ledgars); ParameterFieldDefinitions crParameterFieldDefinitions;
                ParameterFieldDefinition crParameterFieldDefinition;
                ParameterValues crParameterValues = new ParameterValues();
                ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();

                crParameterDiscreteValue.Value = System.Configuration.ConfigurationSettings.AppSettings["CompanyName"];
                crParameterFieldDefinitions = reportobj.DataDefinition.ParameterFields;
                crParameterFieldDefinition = crParameterFieldDefinitions[0];
                crParameterValues = crParameterFieldDefinition.CurrentValues;

                crParameterValues.Clear();
                crParameterValues.Add(crParameterDiscreteValue);
                crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);

                ParameterFieldDefinitions crParameterFieldDefinitions1;
                ParameterFieldDefinition crParameterFieldDefinition1;
                ParameterValues crParameterValues1 = new ParameterValues();
                ParameterDiscreteValue crParameterDiscreteValue1 = new ParameterDiscreteValue();

                crParameterDiscreteValue1.Value = System.Configuration.ConfigurationSettings.AppSettings["CompanyAddress"];
                crParameterFieldDefinitions1 = reportobj.DataDefinition.ParameterFields;
                crParameterFieldDefinition1 = crParameterFieldDefinitions1[1];
                crParameterValues1 = crParameterFieldDefinition1.CurrentValues;

                crParameterValues1.Clear();
                crParameterValues1.Add(crParameterDiscreteValue1);
                crParameterFieldDefinition1.ApplyCurrentValues(crParameterValues1);



                this.crystalReportViewer1.ReportSource = reportobj;
            }
            catch { MessageBox.Show("Some Error Occured while generating Report"); }
        }
    }
}
